home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0078_Sierpinski's Gasket....pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  3KB  |  108 lines

  1. {
  2.   Sierpinski's Gasket using Pascal's Triangle.
  3.   Written by Russ Cox.  June 10, 1994.
  4.  
  5.   Sierpinski's Gasket starts with an equilateral triangle.  /\
  6.                                                           / X  \
  7.                                                         /-------\
  8.  
  9.   This triangle then copies itself and puts a copy to the right and
  10.   at the tip.
  11.  
  12.                                /\
  13.                              / X  \
  14.                            /\------/\
  15.                          / X  \   /X  \
  16.                        /-------\/-------\
  17.  
  18.   It keeps repeating this forever and you get this cool shape, just a lot
  19.   bigger.  This was one of the first fractals.
  20.  
  21.   Blaise Pascal invented what is known as Pascal's Triangle.
  22.  
  23.                                  1
  24.                                 1 1
  25.                                1 2 1
  26.                               1 3 3 1
  27.                              1 4 6 4 1
  28.   etc.
  29.   You start with sides of 1.  As you go down the triangle, to obtain a
  30.   value, you add the numbers above to the left and above to the right.
  31.  
  32.   It just so happens that if you color the pixel for Pascal's Triangle
  33.   as to whether or not the number is odd or even, you get Sierpinski's
  34.   Gasket on your screen.  Have fun!!!
  35.  
  36.   (Feel free to include this in SWAG if you feel like it. I would put it
  37.   in MATH. )
  38.  
  39.      ■ Done! - Kerry ■
  40.  
  41.  
  42.   P.S. If you mess with the right value and leave mid alone... (i.e. make
  43.   right 480 or something, the part that would have been cut off is
  44.   instead folded over on top of the triangle.
  45.  
  46. }
  47.  
  48. program gasket;
  49. uses graph;
  50. var
  51.   grDriver : Integer;
  52.   grMode   : Integer;
  53.   ErrCode  : Integer;
  54. const
  55.    right = 640;
  56.    mid = 320;
  57.    bottom = 256;
  58.  
  59. var
  60.    oddeven : array[1..right] of Boolean;
  61.    c, d, e : integer;
  62.    prevoe  : array[1..right] of Boolean;
  63.  
  64. begin
  65. grDriver := Detect;
  66.   InitGraph(grDriver,grMode,'e:\bp\bgi');
  67.   ErrCode := GraphResult;
  68.   if ErrCode <> grOk then
  69.   begin
  70.     WriteLn('Graphics error:',
  71.             GraphErrorMsg(ErrCode));
  72.     halt(1);
  73.   end;
  74.  
  75.   for c := 1 to right do
  76.       prevoe[ c ] := FALSE;
  77.  
  78.   prevoe[ mid ] := TRUE;
  79.  
  80.   putpixel( mid, 1 , WHITE );
  81.   for c := 2 to bottom do
  82.   begin
  83.        for d := 1 to right do
  84.        begin
  85.            if d = 1 then
  86.                  oddeven[ d ] := prevoe[ d + 1 ]
  87.            else if d = right then
  88. oddeven[ d ] := prevoe[ d - 1 ]
  89.            else
  90.                  oddeven[ d ] := prevoe[ d - 1 ] xor prevoe[ d + 1 ];
  91.  
  92.        if ( d < 640 ) AND ( c < 480 ) then
  93.           if oddeven[ d ] = TRUE then
  94.               putpixel( d, c, WHITE )
  95.           else
  96.               putpixel( d, c, BLACK );
  97.  
  98.        end;
  99.        move( oddeven, prevoe, right );
  100.   end;
  101.  
  102.  
  103. end.
  104.  
  105. {
  106. If you use as a value any power of 2 in the previous program, you get a
  107. full triangle, without bits and pieces falling off.
  108. }